<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/10
 * Time: 15:08
 */

namespace App\Admin\Controllers\Version;

use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Version;

class VersionController extends AdminController
{
    public $title = '版本管理';


    /**
     * 列表
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Version(), function (Grid $grid) {
            $grid->id()->sortable();
            $grid->column('version', '版本号')->editable(true)->sortable();
            $grid->column('type', '类型')->display(function ($type) {
                return Version::TYPE[$type];
            });
            $grid->column('url', '地址');
            $grid->column('is_update', '是否强制更新')->display(function ($receiver_type) {
                return Version::RECEIVER_TYPE[$receiver_type];
            });
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '更新日期');
            $grid->actions(function ($action) {
                //$action->disableEdit();
            });
        });
    }


    /**
     * 表单
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Version());
        $form->text('version', '版本号')->rules('required');
        $form->select('type', '平台类型')->default(1)->options(Version::TYPE)->rules('required');
        $form->url('url', '地址')->rules('required|max:300');
        $form->select('is_update', '是否强制更新')->default(0)->options(Version::RECEIVER_TYPE)->rules('required');
        return $form;
    }


    /**
     * 详情
     * @param $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show($id, new Version());
        $show->id;
        $show->field('version', '版本号');
        $show->field('type', '平台类型')->as(function ($type) {
            return Version::TYPE[$type];
        });
        $show->field('url', '地址');
        $show->field('is_update', '是否强制更新')->as(function ($receiver_type) {
            return Version::RECEIVER_TYPE[$receiver_type];
        });
        $show->disableEditButton();

        return $show;
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        $re_del_message = Version::destroy($id);
        if (!$re_del_message) {
            return response()->json(['status' => false, 'message' => '服务器繁忙！']);
        }
        return response()->json(['status' => true, 'message' => '删除成功']);

    }

}